package me.zingon.wxbus.service;

import me.zingon.wxbus.dao.LineDao;
import me.zingon.wxbus.dao.StationDao;
import me.zingon.wxbus.dao.TimeLogDao;
import me.zingon.wxbus.model.Line;
import me.zingon.wxbus.model.Station;
import me.zingon.wxbus.model.TimeLog;
import me.zingon.wxbus.util.LineUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StationService {


    @Autowired
    LineUtil lineUtil;

    @Autowired
    LineDao lineDao;

    @Autowired
    TimeLogDao timeLogDao;

    @Autowired
    StationDao stationDao;

    //@Scheduled(cron = "0 0/2 7,8,9,16,17,18 * * ? ")
    public void exc() {
        List<Line> lines = lineDao.selectList(null);
        lines.stream().forEach(line -> {
            List<TimeLog> list = lineUtil.list(line);
            list.stream().forEach(tl -> {
                try {
                    timeLogDao.insert(tl);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

    }

    public List<Station> listByLine(Line line) {
        return stationDao.listByLine(line);
    }

    public List<String> listBusNameByLine(Line line) {
        return stationDao.listBusNameByLine(line);
    }

    public List<Map<String, Object>> listSeries(Line line) {
        List<Map<String, Object>> result=new ArrayList<>();
        List<String> busNames = stationDao.listBusNameByLine(line);
        for(String busName:busNames){
            List<Map<String, Object>> temp = stationDao.listByBusname(line.getSegmentId(), busName);
            List<List<List<Object>>> datas = map2list(spiltList(temp));
            result.addAll(getSeries(datas,busName));
        }
        return result;
    }

    public List<Map<String, Object>> getSeries(List<List<List<Object>>> data,String name){
        List<Map<String, Object>> result=new ArrayList<>();
        int i=0;
        for(List<List<Object>> d:data){
            Map<String,Object> m=new HashMap<>();
            m.put("name",name);
            m.put("type","line");
            m.put("data",d);
            result.add(m);
        }
        return result;
    }

    public List<List<List<Object>>> map2list(List<List<Map<String, Object>>> data){
        List<List<List<Object>>> result=new ArrayList<>();
        for(List<Map<String, Object>> l:data){
            List<List<Object>> temp=new ArrayList<>();
            for(Map<String,Object> m:l){
                temp.add(getDot(m));
            }
            result.add(temp);
        }
        return result;
    }

    public List<List<Map<String, Object>>> spiltList(List<Map<String, Object>> line) {
        List<List<Map<String, Object>>> result = new ArrayList<>();
        List<Map<String, Object>> first = new ArrayList<>();
        result.add(first);
        Integer lastOrderno = -1;
        Integer resultNum = 0;
        for (Map<String, Object> map : line) {
            if (lastOrderno.compareTo((Integer) map.get("orderno")) < 0) {
                List<Map<String, Object>> temp = result.get(resultNum);
                temp.add(map);
                lastOrderno = (Integer) map.get("orderno");
            } else if (lastOrderno.compareTo((Integer) map.get("orderno")) > 0) {
                List<Map<String, Object>> temp = new ArrayList<>();
                temp.add(map);
                lastOrderno = (Integer) map.get("orderno");
                result.add(temp);
                resultNum++;
            }
        }
        return result;
    }



    private List<Object> getDot(Map<String,Object> map) {
        List<Object> dot=new ArrayList<>(2);
        dot.add((Integer) map.get("orderno"));
        dot.add(map.get("arrival_time"));
        return dot;
    }
}
